/*
    SEL - Simple DirectMedia Layer Extension Library
    Copyright (C) 2001 Matej Knopp <knopp@users.sf.net>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "events.h"

namespace SEL
{

void 
Event::handleEvents (EventHandler &handler)
{
	SDL::Event event;
	while(SDL::PollEvent(&event)) 
	{
		bool handled = false;
		switch(event.type) 
		{
			case SDL_ACTIVEEVENT:
				handled = handler.handleActiveEvent(
					event.active.gain == 1, 
					event.active.state);
				break;
			case SDL_KEYDOWN:
				handled = handler.handleKeyPressEvent(
					event.key.keysym);
				break;
			case SDL_KEYUP: 
				handled = handler.handleKeyReleaseEvent(
					event.key.keysym);
				break;
			case SDL_MOUSEMOTION: 
				handled = handler.handleMouseMotionEvent(
					event.motion.state,
					event.motion.x,
					event.motion.y,
					event.motion.xrel,
					event.motion.yrel);
				break;
			case SDL_MOUSEBUTTONDOWN:
				handled = handler.handleMouseButtonPressEvent(
					event.button.button,
					event.button.x,
					event.button.y);
				break;
			case SDL_MOUSEBUTTONUP: 
				handled = handler.handleMouseButtonReleaseEvent(
					event.button.button,
					event.button.x,
					event.button.y);
				break;
			case SDL_JOYAXISMOTION: 
				handled = handler.handleJoyAxisEvent(
					event.jaxis.which,
					event.jaxis.axis,
					event.jaxis.value);
				break;
			case SDL_JOYBALLMOTION: 
				handled = handler.handleJoyBallEvent(
					event.jball.which,
					event.jball.ball,
					event.jball.xrel,
					event.jball.yrel);
				break;
			case SDL_JOYHATMOTION:
				handled = handler.handleJoyHatEvent(
					event.jhat.which,
					event.jhat.hat,
					event.jhat.value);
				break;
			case SDL_JOYBUTTONDOWN:
				handled = handler.handleJoyButtonPressEvent(
					event.jbutton.which,
					event.jbutton.button);
				break;
			case SDL_JOYBUTTONUP:
				handled = handler.handleJoyButtonReleaseEvent(
					event.jbutton.which,
					event.jbutton.button);
				break;
			case SDL_QUIT: 
				handled = handler.handleQuitEvent();
				break;
			case SDL_SYSWMEVENT: 
				handled = handler.handleSysWMEvent();
				break;
			case SDL_VIDEORESIZE:
				handled = handler.handleResizeEvent(
					event.resize.w, event.resize.h);
				break;
			case SDL_USEREVENT: 
				handled = handler.handleUserEvent(
					event.user.type,
					event.user.code,
					event.user.data1,
					event.user.data2);
				break;
		}
		if (!handled) 
		{
			handler.handleEvent(event);
		}
	}
}

} // namespace SEL
